package com.cnap.model.strategy.schedule;

import cn.hutool.core.util.IdUtil;
import com.cnap.db.entity.ScheduleStrategyPO;
import com.cnap.model.schedule.engine.DeployPolicyConfigV2;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * v.20，调度策略dto
 *
 * @author xc
 **/
public class ScheduleStrategyDTO {
    // svcId
    private String svcId;
    // load 不为null则说明启用
    private LoadStrategyDTO load;
    // energy 不为null则说明启用
    private EnergyStrategyDTO energy;
    // cost 不为null则说明启用
    private CostStrategyDTO price;
    // sla 不为null则说明启用
    private SLAStrategyDTO sla;
    // 智能决策：1：生效，0：不生效
    private int intelligentScheduleActive;
    // 智能决策周期
    private int intelligentDuration;

    /**
     * 业务场景
     */
    private BusinessScene businessScene;

    /**
     * 服务模型调度（针对gpu）
     */
    private ServiceModelDTO serviceModel;

    /**
     * 位置信息，传空表示全部
     */
    private String location;

    /**
     * get
     *
     * @return Location
     */
    public String getLocation() {
        return location;
    }

    /**
     * set
     *
     * @param location location
     */
    public void setLocation(String location) {
        this.location = location;
    }

    /**
     * get
     *
     * @return ServiceModel
     */
    public ServiceModelDTO getServiceModel() {
        return serviceModel;
    }

    /**
     * set
     *
     * @param serviceModel serviceModel
     */
    public void setServiceModel(ServiceModelDTO serviceModel) {
        this.serviceModel = serviceModel;
    }

    /**
     * get
     *
     * @return BusinessScene
     */
    public BusinessScene getBusinessScene() {
        return businessScene;
    }

    /**
     * set
     *
     * @param businessScene businessScene
     */
    public void setBusinessScene(BusinessScene businessScene) {
        this.businessScene = businessScene;
    }

    /**
     * get
     *
     * @return IntelligentDuration
     */
    public int getIntelligentDuration() {
        return intelligentDuration;
    }

    /**
     * set
     *
     * @param intelligentDuration intelligentDuration
     */
    public void setIntelligentDuration(int intelligentDuration) {
        this.intelligentDuration = intelligentDuration;
    }

    /**
     * get
     *
     * @return SvcId
     */
    public String getSvcId() {
        return svcId;
    }

    /**
     * set
     *
     * @param svcId svcId
     */
    public void setSvcId(String svcId) {
        this.svcId = svcId;
    }

    /**
     * get
     *
     * @return Load
     */
    public LoadStrategyDTO getLoad() {
        return load;
    }

    /**
     * set
     *
     * @param load load
     */
    public void setLoad(LoadStrategyDTO load) {
        this.load = load;
    }

    /**
     * get
     *
     * @return Energy
     */
    public EnergyStrategyDTO getEnergy() {
        return energy;
    }

    /**
     * set
     *
     * @param energy energy
     */
    public void setEnergy(EnergyStrategyDTO energy) {
        this.energy = energy;
    }

    /**
     * get
     *
     * @return Price
     */
    public CostStrategyDTO getPrice() {
        return price;
    }

    /**
     * set
     *
     * @param price price
     */
    public void setPrice(CostStrategyDTO price) {
        this.price = price;
    }

    /**
     * get
     *
     * @return Sla
     */
    public SLAStrategyDTO getSla() {
        return sla;
    }

    /**
     * set
     *
     * @param sla sla
     */
    public void setSla(SLAStrategyDTO sla) {
        this.sla = sla;
    }

    /**
     * get
     *
     * @return IntelligentScheduleActive
     */
    public int getIntelligentScheduleActive() {
        return intelligentScheduleActive;
    }

    /**
     * set
     *
     * @param intelligentScheduleActive intelligentScheduleActive
     */
    public void setIntelligentScheduleActive(int intelligentScheduleActive) {
        this.intelligentScheduleActive = intelligentScheduleActive;
    }

    /**
     * 转换成po
     *
     * @return 结果
     */
    public ScheduleStrategyPO toPO() {
        ScheduleStrategyPO po = new ScheduleStrategyPO();
        po.setId(IdUtil.simpleUUID());
        po.setServiceId(this.svcId);
        po.setLoad(this.load);
        po.setEnergy(this.energy);
        po.setCost(this.price);
        po.setSla(this.sla);
        po.setIntelligentScheduleActive(this.intelligentScheduleActive);
        po.setIntelligentDuration(this.intelligentDuration);
        po.setCreateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));

        po.setBusinessScene(this.businessScene);

        po.setServiceModel(this.serviceModel);

        return po;
    }

    /**
     * 转换成请求配置
     *
     * @return 结果
     */
    public List<DeployPolicyConfigV2> toDeployPolicyConfigV2s() {
        List<CommonScheduleDTO> dtos = Arrays.asList(this.load, this.energy, this.price, this.sla);

        return dtos.stream().filter(Objects::nonNull).map(CommonScheduleDTO::toConfigV2).filter(Objects::nonNull).collect(Collectors.toList());
    }

    /**
     * 校验
     *
     * @return 通过：true，不通过：false
     */
    public boolean verify() {
        //  校验
        // 1. 配置策略百分比加起来需要等于100
        List<CommonScheduleDTO> dtos = Arrays.asList(this.load, this.energy, this.price, this.sla);
        int sum = dtos.stream().filter(Objects::nonNull).mapToInt(CommonScheduleDTO::getPercent).sum();
        return sum == 100;
    }
}
